﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Web.Views.ArchiveBorrow
{
    public partial class ArchiveBorrowMenuManage : PageBase
    {
        /// <summary>
        /// 是否场厂内厂外的借阅逻辑
        /// </summary>
        private bool IsOldArchiveBorrow
        {
            get
            {
                if (string.Equals(Request.QueryString["isBase"], "true", StringComparison.InvariantCultureIgnoreCase))
                {
                    return false;
                }
                return true;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        protected void BindGrid()
        {
            DataTable dt = GetArchiveRoles();
            this.gv_MenuManage.DataSource = PageDataList.GetPagedTable(dt, pager.CurrentPageIndex, pager.PageSize);
            pager.RecordCount = dt.Rows.Count;
            this.gv_MenuManage.DataBind();
        }


        private DataTable GetArchiveRoles()
        {
            try
            {
                string roleid = this.IsOldArchiveBorrow ? "51,52,53,54" : "111,112,113,114";
                //查看拥有档案录入、审核、上交，接收的菜单的权限的角色
                string Sql = string.Format(@"select t.User_ID, t.User_Name, t.OrgName, m.menu
  from (select distinct u.user_ID,
                        u.user_name,
                        (select Org_Name
                           from p_auth_org g
                          where g.org_id = u.org_id) as OrgName
          from p_auth_user u
         inner join p_auth_user_role r
            on u.user_id = r.user_id
         where r.role_id in ({0})) t
  left join (select t.User_ID, WMSYS.WM_CONCAT(to_char(menu)) as menu --行转列               
               from (select a.User_ID,
                            (select ArchiveMenu
                               from archivemenu ar
                              where ar.typeid = a.archivemenuid) as menu
                       from ARCHIVEBORROWMENU a) t
              group by t.User_ID) m
    on t.user_id = m.User_ID", roleid);
                IMDataHelper dal = new IMDataHelper();
                DataTable dt = dal.DBHelper.ExecuteDataSet(Sql).Tables[0];
                return dt;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        //翻页事件
        protected void pager_PageChanging(object sender, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            pager.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        /// <summary>
        /// 编辑事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gv_MenuManage_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string pkValue = this.gv_MenuManage.DataKeys[e.NewEditIndex].Value.ToString();
            this.Response.Redirect("ArchiveBorrowMenuManageEdit.aspx?User_ID=" + pkValue + "&isBase=" + (IsOldArchiveBorrow ? "false" : "true"));
        }
    }
}